using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Collections.Generic;
using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Imaging;
using Sce.PlayStation.Core.Environment;
using Sce.PlayStation.HighLevel.UI;
using VitaReader;

namespace VitaReaderLibPS
{
	internal enum MainMenuStatus : int
	{
		MS_START = -1,
		MS_CONTINUE = 0,
		MS_BOOKS = 1,
		MS_SETTING = 2,
		MS_HELP = 3,
		MS_EXIT = 4,
		
		MS_COUNT = 5,
	};
	
	
    internal class MainMeu : ReaderUI
    {
		private ImageBox		mBkImage;
		private MainMenuStatus	mMainMenuStatus;
		private List<Button>	mMainMenuButtonList;
		private EventHandler< TouchEventArgs >[]	mButtonCommand;
		
		#region readerUI overrides
		public override void Initialize(ReaderScene scene)
		{
			base.Initialize(scene);
			
			#region create
            mRoot = new Panel();
            mRoot.Name = "MainMenuRoot";
			mRoot.BackgroundColor = mScene.ListBackgroundColor;
			mBkImage = new ImageBox();
			mBkImage.ImageScaleType = ImageScaleType.Stretch;
			mBkImage.Image = mScene.BackgroundImage;
			mBkImage.Visible = true;
			mRoot.AddChildFirst(mBkImage);
			//mRoot.BackgroundStyle = DialogBackgroundStyle.Custom;
			//mRoot.CustomBackgroundColor = new UIColor(0,0,0,0.65f);//mScene.ListBackgroundColor;
			#endregion
			
			#region init
			DragGestureDetector drag = new DragGestureDetector();
			drag.MaxDistance = 10;
			drag.DragDetected += HandleMainMenuDrag;
			mBkImage.AddGestureDetector(drag);
			
			mRoot.KeyEventReceived += HandleMainMenuKey;
			mMainMenuButtonList = new List<Button>( (int)MainMenuStatus.MS_COUNT );
			mButtonCommand = new EventHandler< TouchEventArgs >[ (int)MainMenuStatus.MS_COUNT ];
			
			//mButtonCommand[(int)MainMenuStatus.MS_CONTINUE]
			mButtonCommand[(int)MainMenuStatus.MS_CONTINUE] += mScene.HandleContinue;
			mButtonCommand[(int)MainMenuStatus.MS_BOOKS] += mScene.HandleBookList;
			mButtonCommand[(int)MainMenuStatus.MS_HELP] += mScene.HandleHelp;
			mButtonCommand[(int)MainMenuStatus.MS_EXIT] += mScene.HandleExit;
			
			for(int i = (int)MainMenuStatus.MS_START+1; i < (int)MainMenuStatus.MS_COUNT; ++i)
			{
				if( i == (int)MainMenuStatus.MS_SETTING )
				{
					mMainMenuButtonList.Add(null);
					continue;
				}
				
				Button button = new Button();
				button.TextColor = mScene.ButtonTextColor;
            	button.TextFont = mScene.ButtonTextFont;
            	button.TextTrimming = TextTrimming.None;
            	button.Style = ButtonStyle.Custom;
            	button.CustomImage = mScene.ButtonImage;
				button.ButtonAction += this.HandleMainmenuButton;
				button.Name = "MainMenuButton." + i;
				//button.FocusStyle = FocusWidget
				//button.Focusable = false;
				mMainMenuButtonList.Add(button);
				mRoot.AddChildLast(button);
			}
			
			mMainMenuStatus = MainMenuStatus.MS_CONTINUE;
			MenuRefresh();
			#endregion
		}
		
		public override void UpdateLayout(LayoutOrientation orientation)
		{
			switch (orientation)
            {
            case LayoutOrientation.Vertical:
                break;

            default:
				//main menu
				mRoot.SetPosition(0, 0);
                mRoot.SetSize(mScene.View.Width, mScene.View.Height);
                mRoot.Anchors = Anchors.Top | Anchors.Height | Anchors.Left | Anchors.Width;
				mRoot.Focusable = false;
				
				mBkImage.SetPosition(0,0);
				mBkImage.SetSize(mScene.View.Width,mScene.View.Height);
				
				
				float btnPadding = mScene.View.ButtonPadding;
				float btnWidth = mScene.View.ButtonWidth;
				float btnHeight = mScene.View.ButtonHeight;
				float btnSpacingV = 10.0f;
				
				//buttons on main menu: from bottom to top
				float btnHPos = mScene.View.Width-btnPadding-btnWidth;
				float btnVPos = mScene.View.Height-btnPadding-btnHeight;
				
				for(int i = (int)MainMenuStatus.MS_COUNT-1; i > (int)MainMenuStatus.MS_START; --i)
				{
					Button button = mMainMenuButtonList[i];
					if( button == null )
						continue;
					
					button.SetPosition(btnHPos, btnVPos);
                	button.SetSize(btnWidth, btnHeight);
                	button.Anchors = Anchors.Top | Anchors.Height | Anchors.Left | Anchors.Width;
                	button.Visible = true;
					
					btnVPos -= btnSpacingV + btnHeight;
				}
				break;
            }//switch
		}
		
		public override void UpdateLanguage(StreamReader reader)
		{
			String s = reader.ReadLine();
			String[] items = s.Split(',');
			for(int i = 0; i < items.Length; ++i)
			{
				if( i >= mMainMenuButtonList.Count )
					break;
				Button button = mMainMenuButtonList[i];
				if( button == null )
					continue;
				button.Text = items[i];
			}
		}

		#endregion
		
		void MenuRefresh()
		{
			for(int i = 0; i < (int)MainMenuStatus.MS_COUNT; ++i)
			{
				if( mMainMenuButtonList[i] != null )
				{
					if( i == (int)mMainMenuStatus )
					{
						mMainMenuButtonList[i].CustomImage = mScene.HilightButtonImage;
						mMainMenuButtonList[i].SetFocus(true);
					}
					else
						mMainMenuButtonList[i].CustomImage = mScene.ButtonImage;
				}
			}
		}
		
		void MainMenuPrev()
		{
			while(true)
			{
				if( --mMainMenuStatus == MainMenuStatus.MS_START )
				{
					mMainMenuStatus = MainMenuStatus.MS_EXIT;
					//mMainMenuStatus = MainMenuStatus.MS_START+1;
					break;
				}
				if( mMainMenuButtonList[ (int)mMainMenuStatus ] != null )
					break;
			}
			MenuRefresh();
		}
		
		void MainMenuNext()
		{
			while(true)
			{
				if( ++mMainMenuStatus == MainMenuStatus.MS_COUNT )
				{
					mMainMenuStatus = MainMenuStatus.MS_CONTINUE;
					//mMainMenuStatus = MainMenuStatus.MS_COUNT-1;
				}
				if( mMainMenuButtonList[ (int)mMainMenuStatus ] != null )
					break;
			}
			MenuRefresh();
		}
		
		void HandleMainmenuButton(object sender, TouchEventArgs e)
		{
			Button button = sender as Button;
			String[] s = button.Name.Split('.');
			if( s.Length < 2 )
				return;
			int index = Convert.ToInt32(s[1]);
			if( index <= (int)MainMenuStatus.MS_START || index >= (int)MainMenuStatus.MS_COUNT )
				return;
			mMainMenuStatus = (MainMenuStatus)index;
			MenuRefresh();
			MainMenuInvoke();
		}
		
		void MainMenuInvoke()
		{
			Button button = mMainMenuButtonList[ (int)mMainMenuStatus ];
			EventHandler< TouchEventArgs > handler = mButtonCommand[  (int)mMainMenuStatus ];
			if( button != null && handler != null)
			{
				handler.Invoke(button, new TouchEventArgs(new TouchEventCollection() ));
			}
		}
		
		void HandleMainMenuKey(object sender, KeyEventArgs e)
		{
			if( e.KeyEventType == KeyEventType.Up )
			{
				switch(e.KeyType)
				{
				case KeyType.Enter:
					MainMenuInvoke();
					break;
				case KeyType.Up:
				case KeyType.Left:
					MainMenuPrev();
					break;
				case KeyType.Down:
				case KeyType.Right:
					MainMenuNext();
					break;
				case KeyType.L:
					//L/R switch between reading/mainmenu/bookmarks
					mScene.Handler.ReadingContinue(UISwitch.RIGHT);
					break;
				}
			}
		}
		
		void HandleMainMenuDrag(object sender, DragEventArgs e)
		{
			DragType drag = mScene.Control.DetectDrag(e.Distance.X,e.Distance.Y);
			//horizontal drag gesture, same as keypad R
			if( drag == DragType.LEFT )
				mScene.Handler.ReadingContinue(UISwitch.RIGHT);
		}
				
	}//class
}